import random
import time
import tkinter
import pygame
from tkinter import font

pygame.mixer.init()
sound1=pygame.mixer.Sound('滚动.wav')
sound2=pygame.mixer.Sound('完成.wav')

root=tkinter.Tk()
root.title('必背60首古诗词')
root.resizable(False,False)
title=tkinter.Label(root,text='语文必背古诗词60首抽背',font=("宋体",25,'bold'),bg='white')
title.pack(pady=10)
root.config(bg='white')



def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size) 
center_window(root,800,280)

def chushihua():
    global names
    global select
    names=["《论语》十二章","劝学","屈原列传","谏太宗十思疏","师说","阿房宫赋","六国论","答司马谏议书","赤壁赋","项脊轩志","子路、曾皙、冉有、公西华侍坐","报任安书","过秦论（上）","礼运","陈情表","归去来兮辞（并序）","种树郭橐驼传","五代史伶官传序","石钟山记","登泰山记","静女","无衣","离骚","涉江采芙蓉","短歌行","归园田居（其一）","拟行路难（其四）","春江花月夜","山居秋暝","蜀道难","梦游天姥吟留别","将进酒","燕歌行","蜀相","客至","登高","登岳阳楼","琵琶行（并序）","李凭箜篌引","菩萨蛮（小山重叠金明灭）","锦瑟","虞美人","望海潮","桂枝香·金陵怀古","江城子·乙卯正月二十日夜记梦","念奴娇·赤壁怀古","登快阁","鹊桥仙","苏幕遮","声声慢","书愤","临安春雨初霁","念奴娇·过洞庭","永遇乐·京口北固亭怀古","菩萨蛮·书江西造口壁","青玉案·元夕","贺新郎（国脉微如缕）","扬州慢（淮左名都）","长亭送别","朝天子·咏喇叭"]
    select=[]

chushihua()   

name=tkinter.StringVar()
name.set('请选择课文数量')
num=0
key=0
t=0

def startname():
        global i
        global name
        global names
        global t
        global selectlist
        global suiji
        global select
        global huifu
        global key

        i=0
        t=0
        starttime = time.time()
        select=[]
        namedel()
        jian.config(state='disabled')
        jia.config(state='disabled')
        start.config(state='disabled')
        
        if len(names)<num:
             chushihua()
             return
        if num==0:
                selectlist.pack_forget()
                scrollbar.pack_forget()
                suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
                name.set('请选择课文数量')
        selectlist.pack_forget()
        scrollbar.pack_forget()
        while num!=0:
            root.after(160, root.update())
            name.set(random.choice(names))
            endtime = time.time()
            if i!=num:
                select.append(name.get())
                names.remove(name.get())
                t+=1
                i+=1
            if endtime-starttime>3:
                break
            sound1.play()
            root.update()
        jian.config(state='normal')
        jia.config(state='normal')
        start.config(state='normal')
        if num!=0 and num>1:
            name.set('请点击“开始”进行抽选')
            newwindow()
        elif num==0:
            name.set('请选择课文数量')
        else:
            name.set('，'.join(select))
        jian.config(state='normal')
        jia.config(state='normal')
        start.config(state='normal')

        
def newwindow():
    global new_root
    global num
    new_root = tkinter.Tk()
    new_root.title('名单')
    new_root.config()
    new_root.resizable(False,False)

    def center_window(new_root, width, height):
        screenwidth = new_root.winfo_screenwidth()
        screenheight = new_root.winfo_screenheight()
        size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        new_root.geometry(size)
    if num>=5 and num<10: 
        hight=34.5*num
    elif num>=10:
        hight=33.5*num
    else:
        hight=35*num
    center_window(new_root,600,hight)

    sound2.play()

    selectlist=tkinter.Text(new_root,font=("宋体",25,'bold'),relief=tkinter.FLAT,bg='white')
    selectlist.pack(expand=True)

    text_font = font.Font(family="宋体", size=25, weight='bold')
    max_width = 600

    i=0
    for index, name in enumerate(select):
        text_width = text_font.measure(name)
        spaces = ' ' * ((max_width - text_width) // (2 * text_font.measure(' ')))
        if num-i==1:
            selectlist.insert(f'{index+1}.0', spaces + name)
        else:
            selectlist.insert(f'{index+1}.0', spaces + name + '\n')
            i+=1

    selectlist.config(state=tkinter.DISABLED)



    new_root.mainloop()

def namedel():
        global selectlist
        global t
        global names
        global suiji
        global repoint_var
        root.after_cancel
        chushihua()
        selectlist.config(state=tkinter.NORMAL)
        t=0
        selectlist.delete(0.1,tkinter.END)
        selectlist.config(state=tkinter.DISABLED)
     
def recovery():
        global key
        chushihua()
        suiji.pack_forget()
        key=0
        suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
        if num!=0 and num>1:
            name.set('请点击“开始”进行抽选')
        if num==0:
            name.set('请选择课文数量')
        start.config(state='normal')

def gettime():
      timestr = time.strftime("%H:%M:%S")
      lb.configure(text=timestr)
      root.after(1000,gettime)

def numplus():
        global num
        global geshu
        global name
        global selectlist
        global scrollbar
        selectlist.pack_forget()
        scrollbar.pack_forget()
        if num<15:
            num+=1
            geshu.destroy()
            geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
            geshu.pack(side=tkinter.LEFT)
            jian.config(state=tkinter.NORMAL)
        if num==15:
             jia.config(state=tkinter.DISABLED)
        if num!=0:
            name.set('请点击“开始”进行抽选')

def numminus():
        global num
        global geshu
        global jian
        global jia
        global selectlist
        global scrollbar
        selectlist.pack_forget()
        scrollbar.pack_forget()
        if num>0:
            num-=1
            geshu.destroy()
            geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
            geshu.pack(side=tkinter.LEFT)
            jia.config(state=tkinter.NORMAL)
        if num==0:
            jian.config(state=tkinter.DISABLED)
            name.set('请选择课文数量')
        if num!=0:
            name.set('请点击“开始”进行抽选')


mingzi=tkinter.Frame(root,bg='white')
suiji=tkinter.Label(mingzi,textvariable=name,font=("宋体",30,'bold'),relief=tkinter.FLAT,bg='white')
suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
selectlist=tkinter.Text(mingzi,font=("宋体",20,'bold'),relief=tkinter.FLAT,width=24, height=4,state=tkinter.DISABLED,bg='white')
scrollbar=tkinter.Scrollbar(mingzi,command=selectlist.yview,bg='white')
mingzi.pack(pady=20)



anniu=tkinter.Frame(root,bg='white')

renshu=tkinter.Label(anniu,text='数量:',font=("宋体",20,'bold'),bg='white')
renshu.pack(side=tkinter.LEFT,padx=50)

jian=tkinter.Button(anniu,text='-',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=numminus,state=tkinter.DISABLED)
jian.pack(side=tkinter.LEFT)

geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
geshu.pack(side=tkinter.LEFT)

jia=tkinter.Button(anniu,text='+',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=numplus)
jia.pack(side=tkinter.RIGHT)

start=tkinter.Button(anniu,text='开始',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=startname)
start.pack(side=tkinter.RIGHT,padx=50,before=jia)
anniu.pack()


lb = tkinter.Label(root,text='',font=("宋体",15),bg='white')
lb.pack(side=tkinter.LEFT)
gettime()


shuiyin=tkinter.Label(root,text='by Fengchunye',font=("宋体",15),bg='white')
shuiyin.pack(side=tkinter.RIGHT)

root.mainloop()